library(zoo)
library(xts)
#smooth the data 
smoothData <- function(timeSeries, MeanDepth, IndexTimeSeries) {
  ret <- as.vector(rollapply(timeSeries, MeanDepth, mean, align="right"))
  return(as.xts(ret,order.by=IndexTimeSeries[MeanDepth:length(IndexTimeSeries)]))
}

# plot(zf)
# plot(smoothData(zf,4,IndexZf))

# AR on smoothed data
ARSmoothedPredict <- function(maxAROrder, ARtype, MeanDepth) {
  return(
    function(timeSeries){ 
      smoothedTimeSeries = smoothData(timeSeries,MeanDepth,index(timeSeries))
      options(warn=-1)
      sim <- ar(x=smoothedTimeSeries, order.max=maxAROrder, method=ARtype, demean=FALSE)
      options(warn=1)
      #gc()
      return((predict(sim, n.ahead=1)$pred)[1])
    })
}

for (MeanDepth in 1:20){
f<-ARSmoothedPredict(3,'ols',MeanDepth)
  ret<-ownPredict(google,f,10000,logDiffTransform, logDiffTransform)
  #plot(ret$x2)
  #lines(ret$y2)
  print(ret$aafe)
}